//
//  CJImagesTableViewCell1.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 2016/06/18.
//  Copyright © 2016年 dvlproad. All rights reserved.
//

#import "CJImagesTableViewCell1.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "CQTSLocImagesUtil.h"

#import "UIImageView+AsyncDownload.h"

@interface CJImagesTableViewCell1 () {
    
}

@end



@implementation CJImagesTableViewCell1

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView = ({
            UIView *view = [UIView new];
            view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0]; //#f4f4f4
            view;
        });
        
        [self setupViews];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}


#pragma mark - setupViews
- (void)setupViews {
    [self addSubview:self.titleNameLabel];
    [self addSubview:self.iconImageView];
    [self addSubview:self.messageTipLabel];

    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(10);
        make.width.mas_equalTo(self.iconImageView.mas_height).multipliedBy(1.0);
        make.top.mas_equalTo(self).mas_offset(0);
        make.centerY.mas_equalTo(self);
    }];
    
    [self.titleNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right);
        make.right.mas_equalTo(self).mas_offset(-10);
        make.centerY.mas_equalTo(self).mas_equalTo(-10);
        make.height.mas_equalTo(17);
    }];
    
    
    [self.messageTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleNameLabel);
        make.right.mas_equalTo(self.titleNameLabel);
        
        make.centerY.mas_equalTo(self).mas_equalTo(10);
        make.height.mas_equalTo(14);
    }];
}

- (void)setImageModel:(CQTSNetImageDataModel *)moduleModel
      shouldLoadImage:(BOOL)shouldLoadImage
        withIndexPath:(NSIndexPath *)indexPath
{
    self.titleNameLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    self.messageTipLabel.text = moduleModel.name;
    
    UIImage *placeholderImage = [CQTSLocImagesUtil cjts_placeholderImage01];
    if (shouldLoadImage == NO) {
        [self.iconImageView setImage:placeholderImage];
    } else {
//        方法1：
//        if (moduleModel.haveLoadImage == NO) {
//            [self.iconImageView setImage:placeholderImage];
//
//            NSURL *imageURL = [NSURL URLWithString:moduleModel.imageUrl];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    NSLog(@"%@", moduleModel.name);
//                    self.iconImageView.image = image;
//
//                    moduleModel.haveLoadImage = YES;
//                    moduleModel.imagePlaceholderImage = image;
//                });
//            });
//        } else {
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                UIImage *image = moduleModel.imagePlaceholderImage;
//
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    NSLog(@"%@", moduleModel.name);
//                    self.iconImageView.image = image;
//                });
//            });
//            //[self.iconImageView setImage:moduleModel.imagePlaceholderImage];
//        }
        
//        方法2：
        NSURL *imageURL = [NSURL URLWithString:moduleModel.imageUrl];
        [self.iconImageView setImageWithURL:imageURL placeholderImage:placeholderImage];
        
//        方法3：
//        NSURL *imageURL = [NSURL URLWithString:moduleModel.imageUrl];
//        [self.iconImageView sd_setImageWithURL:imageURL completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//            NSLog(@"%@", moduleModel.name);
//            moduleModel.haveLoadImage = YES;
//        }];
    }
}

#pragma mark - lazy init
@synthesize titleNameLabel = _titleNameLabel;
- (UILabel *)titleNameLabel {
    if (!_titleNameLabel) {
        _titleNameLabel = [[UILabel alloc] init];
        _titleNameLabel.backgroundColor = [UIColor clearColor];
        _titleNameLabel.textColor =  [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0];  // (@"#333333");
        _titleNameLabel.font = [UIFont systemFontOfSize:17];
        _titleNameLabel.textAlignment = NSTextAlignmentLeft;
        //_titleNameLabel.adjustsFontSizeToFitWidth = YES; // 字号不允许进行改变
    }
    return _titleNameLabel;
}

@synthesize iconImageView = _iconImageView;
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.layer.cornerRadius = 6;
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.backgroundColor = [UIColor clearColor];
    }
    return _iconImageView;
}

@synthesize messageTipLabel = _messageTipLabel;
- (UILabel *)messageTipLabel {
    if (!_messageTipLabel) {
        _messageTipLabel = [[UILabel alloc] init];
        _messageTipLabel.backgroundColor = [UIColor redColor]; //#ff0000
        _messageTipLabel.textColor = [UIColor whiteColor];  // (@"#FFFFFF");
        _messageTipLabel.font = [UIFont systemFontOfSize:14];
        _messageTipLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _messageTipLabel;
}



@end
